package com.link.serve.push.grpc;

import com.link.serve.push.grpc.ChatMessageProto.SendMessageRequest;
import com.link.serve.push.grpc.ChatMessageProto.SendMessageResponse;
import com.link.serve.push.grpc.handler.ChatMessagePushProcessor;
import com.link.serve.utils.SpringContextHolder;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

@GrpcService
public class MessageServiceImpl extends MessageServiceGrpc.MessageServiceImplBase {
    @Override
    public void sendMessage(SendMessageRequest request, StreamObserver<SendMessageResponse> responseObserver) {
        ChatMessageProto.ChatMessage message = request.getMessage();
        // 在这里可以进行处理，例如将消息发送到 Kafka 或者存入数据库 这里进行推送到各个服务里面
        ChatMessagePushProcessor chatMessagePushProcessor = SpringContextHolder.getBean(ChatMessagePushProcessor.class);
        chatMessagePushProcessor.chatMessage(message);
        // 返回响应
        SendMessageResponse response = SendMessageResponse.newBuilder()
                .setSuccess(true)
                .build();

        // 将响应发送给客户端
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}
